package com.hbm.usercenter.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;

import butterknife.Bind;

/**
 * Created by BS on 2017/6/1.
 */

public class GroupOption extends RelativeLayout {

    ImageView iv_icon;
    TextView tv_text;
    @Bind(R.id.rl)
    RelativeLayout mRl;

    public GroupOption(Context context) {
        super(context);
        initView(context);
    }


    public GroupOption(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initTypedArray(context, attrs);
    }

    public GroupOption(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initTypedArray(context, attrs);
    }

    private void initView(Context context) {
        if (!isInEditMode()) {
            LayoutInflater.from(context).inflate(R.layout.group_option, this, true);

            iv_icon = (ImageView) findViewById(R.id.iv_icon);
            tv_text = (TextView) findViewById(R.id.tv_text);
            mRl = (RelativeLayout) findViewById(R.id.rl);
        }
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupOption);

        if (null != typedArray) {
            int iconId = typedArray.getResourceId(R.styleable.GroupOption_group_icon, R.mipmap
                    .icon_my_information);
            String group_text = typedArray.getString(R.styleable.GroupOption_group_text);
            if (!isInEditMode()) {
                if (iconId != -1) {
                    iv_icon.setBackgroundResource(iconId);
                }
                if (!TextUtils.isEmpty(group_text)) {
                    tv_text.setText(group_text);
                }
            }

        }
        typedArray.recycle();
    }

    public void setclickListener(OnClickListener onclickListener) {
        mRl.setOnClickListener(onclickListener);
    }

}
